var _memberIds = new Array();
var _ruleIds = new Array();
var _ruleNums = new Array();
var _shopIds = new Array();
var _shopId = '';
var _couponResponseCache = null;
$(function() {
	$('.doubledate').kuiDate({
		className : 'doubledate',
		isDisabled : "0"
	});
	initShopSelecter();
});

/**
 * 栏目选择器
 */
function columnSelecter(self) {
	$(self).blur();
	initSelecter({
		method : 'get',
		url : '../column/selecter',
		heads : ["columnId", "columnName"],
		titles : ["栏目Id", "栏目名称"],
		data :{"type":2},
		zindex : 500,
		callBack : function(response, row) {
			var value = response.getValue('columnId', row);
			var key = response.getValue('columnName', row);
			var option = $("<option value='" + value + "'>" + key + "</option>");
			$(self).empty().append($(option));
			var addCoupons = document.getElementById("addCoupons");
			selectCoupon(addCoupons);
		}
	});
}

/**
 * 初始化店面选择器
 */
function initShopSelecter() {
	refreshInput($(".serWrapper"));
	$("#shopIsChoosed").removeAttr("checked");
	$("#shopName").attr("disabled", "disabled");

	$("#shopIsChoosed").bind("click", function() {
		var checked = $(this).attr("checked");
		if (checked) {
			$("#shopName").removeAttr("disabled");
		} else {
			$("#shopName").attr("disabled", "disabled");
			$("#shopName").empty();
		}
	});
}

/**
 * 查询
 */
function search() {

	var shopId = $("#shopName").val();
	var startTime = $("#startTime").val();
	var endTime = $("#endTime").val();
	var mp = $("#searchMp").val();
	var type = $("#searchType").val();

	var data = {
		shopId : shopId,
		startTime : startTime,
		endTime : endTime,
		mp : mp,
		type : type
	};

	data = qmsk.cleanObject(data);

	_pagination = new Pagination({
		url : '../memberCouponBill/give/list',
		data : data,
		success : printListToPage,
		pagerHandler : 'pager'
	});
}

/**
 * 显示
 */
function printListToPage(rst) {
	//"shopID","shopName","ruleID","type","couponNo","customerName",
	//"mp","couponCount","faceValue","createFileTime",
	//"uneffectDate","billFrom","status"
	var response = new qmsk.Response(rst);
	qmsk.dataTable({
		tbodyId : 'dataList',
		heads : ["shopName", "type", "couponNo", "memberCardNo", "customerName", "mp", "couponCount", "faceValue", "createFileTime", "UneffectDate", "billFrom", "status"],
		response : response,
		sup : ['sup', '共 [index] 条']
	});
}

function showGive() {

	_memberIds.length = 0;
	_ruleIds.length = 0;
	_ruleNums.length = 0;
	$("#addShopName").empty();
	$("#memberListData").empty();
	$("#couponListData").empty();
	$("#addCoupons").empty();
	$("#addNum").val('');
	$("#addMp").val('');
	_memberIds.length = 0;

	$("#givePanel").show();
	$("#dataListPanel").hide();
}

function closeGive() {
	$("#givePanel").hide();
	$("#dataListPanel").show();
}

function selectCoupon(self) {

	var columnId = $("#addColumn").val();
	if (qmsk.isEmpty(columnId)) {
		artAlert('请选择栏目！');
		return;
	}
	$(self).blur();
	initSelecter({
		url : '../couponRule/selecter',
		data : {
			columnId : columnId
		},
		heads : ["ruleId", "shopName", "ruleName", "totalNum", "usedNum"],
		titles : ["优惠券Id", "店面", "优惠券名称", "券数量", "已发放"],
		callBack : callBack
	});

	function callBack(response, i) {
		_couponResponseCache = response;
		//"ruleId","ruleName","shopId","type","totalNum","usedNum","EffectDate","UneffectDate"
		var shopId = response.getValue("shopId", i);
		_shopId = shopId;
		var key = response.getValue("ruleName", i);
		var option = "<option selected='selected' value='" + i + "'>" + key + "</option>";
		$(self).empty();
		$(self).append($(option));
	}

}

function addCoupon() {

	var row = $("#addCoupons").val();
	if (qmsk.isEmpty(row)) {
		artAlert("请选择优惠券！");
		return;
	}
	var num = $("#addNum").val();
	var intPattern = /^[0-9]*[1-9][0-9]*$/;
	if (!intPattern.test(num)) {
		artAlert("请输入正确的数量！");
		return;
	}
	var response = _couponResponseCache;
	var ruleId = response.getValue("ruleId", row);
	var i = 0;
	for (; i < _ruleIds.length; i++) {
		if (ruleId == _ruleIds[i]) {
			break;
		}
	}
	if (i < _ruleIds.length) {
		artAlert('该优惠券已选！');
		return;
	}

	var shopName = response.getValue("shopName", row);
	var ruleName = response.getValue("ruleName", row);
	var totalNum = response.getValue("totalNum", row);
	var usedNum = response.getValue("usedNum", row);

	var $tbody = $("#couponListData");
	var $tr = $("<tr id='couponTr_" + ruleId + "'></tr>");
	$tbody.append($tr);
	var $td0 = $("<td>" + shopName + "</td>");
	var $td1 = $("<td>" + ruleId + "</td>");
	var $td2 = $("<td>" + ruleName + "</td>");
	var $td3 = $("<td>" + totalNum + "</td>");
	var $td4 = $("<td>" + usedNum + "</td>");
	var $td5 = $("<td>" + num + "</td>");
	var $td6 = $("<td><input type='button' onClick='deleteCoupon(" + ruleId + ")' class='common_btn del' value='删除' /></td>");
	$tr.append($td0);
	$tr.append($td1);
	$tr.append($td2);
	$tr.append($td3);
	$tr.append($td4);
	$tr.append($td5);
	$tr.append($td6);

	_ruleIds.push(ruleId);
	_ruleNums.push(num);
	_shopIds.push(_shopId);
}

function deleteCoupon(ruleId) {
	$("#couponTr_" + ruleId).remove();
	var i = 0;
	for (; i < _ruleIds.length; i++) {
		if (_ruleIds[i] == ruleId) {
			break;
		}
	}
	if (i < _ruleIds.length) {
		for (var j = i; j < _ruleIds.length; j++) {
			_ruleIds[j] = _ruleIds[j + 1];
			_ruleNums[j] = _ruleNums[j + 1];
			_shopIds[j] = _shopIds[j + 1];
		}
		_ruleIds.length = _ruleIds.length - 1;
		_ruleNums.length = _ruleNums.length - 1;
		_shopIds.length = _shopIds.length - 1;
	}
}

function selectMember(self) {
	var shopId = $("#addShopName").val();
	if (shopId == null) {
		shopId = '';
	}
	$(self).blur();
	initSelecter({
		url : '../member/selecter',
		data : {
			shopId : shopId
		},
		heads : ["membeId", "customerName", "mp"],
		titles : ["会员Id", "会员名称", "手机"],
		callBack : callBack
	});
	function callBack(response, row) {
		var key = response.getValue("membeId", row);
		var value = response.getValue("customerName", row);
		var mp = response.getValue("mp", row);
		$("#addMp").val(mp);
	}

}

function addMember() {
	var mp = $("#addMp").val();
	if (qmsk.isEmpty(mp)) {
		artAlert('请输入手机号码，或者选择会员！');
		return;
	}

	if (document.getElementById("tr_" + mp)) {
		artAlert("该手机号码已经添加！");
		return;
	}

	qmsk.get('../member/find/memberInfo', {
		mp : mp
	}, function(response) {
		//["memberCardNo","memberId","customerName","mp"
		if (response.row <= 0) {
			artAlert('手机号码不存在！');
			return;
		}
		var memberCardNo = response.getValue("memberCardNo");
		var memberId = response.getValue("memberId");
		var customerName = response.getValue("customerName");
		var mp = response.getValue("mp");

		var $tr = $("<tr id='tr_" + mp + "'></tr>");
		var $td1 = $("<td>" + memberCardNo + "</td>");
		var $td2 = $("<td>" + customerName + "</td>");
		var $td3 = $("<td>" + mp + "</td>");
		var $td4 = $("<td><input type='button' onClick='delteMember(" + mp + "," + memberId + ")' class='common_btn del' value='删除' /></td>");

		$tr.append($td1);
		$tr.append($td2);
		$tr.append($td3);
		$tr.append($td4);

		$("#memberListData").append($tr);
		_memberIds.push(memberId);
	});
}

function delteMember(mp, memberId) {
	$("#tr_" + mp).remove();
	var i = 0;
	for (; i < _memberIds.length; i++) {
		if (_memberIds[i] == memberId) {
			break;
		}
	}
	if (i < _memberIds.length) {
		for (var j = i; j < _memberIds.length; j++) {
			_memberIds[j] = _memberIds[j + 1];
		}
		_memberIds.length = _memberIds.length - 1;
	}
}

function giveCoupon() {
	if (qmsk.isEmpty(_memberIds) || _memberIds.length == 0) {
		artAlert("请选择会员！");
		return;
	}
	if (qmsk.isEmpty(_ruleIds) || _ruleIds.length == 0) {
		artAlert("请选择优惠券！");
		return;
	}
	artConfirm("确定发放吗？", function() {

		// 会员Id
		var ids = '';
		var i = 0;
		for (; i < _memberIds.length - 1; i++) {
			ids = ids + _memberIds[i] + ', ';
		}
		ids = ids + _memberIds[i];

		// 优惠券Id
		var couponIds = '';
		var i = 0;
		for (; i < _ruleIds.length - 1; i++) {
			couponIds = couponIds + _ruleIds[i] + ', ';
		}
		couponIds = couponIds + _ruleIds[i];

		//优惠券数量
		var ruleNums = '';
		var i = 0;
		for (; i < _ruleNums.length - 1; i++) {
			ruleNums = ruleNums + _ruleNums[i] + ', ';
		}
		ruleNums = ruleNums + _ruleNums[i];

		//店面 _shopIds
		var shopIds = '';
		var i = 0;
		for (; i < _shopIds.length - 1; i++) {
			shopIds = shopIds + _shopIds[i] + ', ';
		}
		shopIds = shopIds + _shopIds[i];

		qmsk.get('../memberCouponBill/give', {
			memberIds : ids,
			ruleIds : couponIds,
			ruleNums : ruleNums,
			shopIds : shopIds
		}, function(response) {
			artSuccess("发放成功！");
			var old = $("#addCouponUsedAmount").val();
			_memberIds.length = 0;
			_ruleIds.length = 0;
			$("#memberListData").empty();
			$("#couponListData").empty();
		});
	});
}